package cn.jbt.api.admin.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "sys_config", schema = "jbt")
public class SysConfig {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @NotBlank(message = "配置索引不能为空")
    @Column(name = "uniq_idx", nullable = false, length = 64)
    private String uniqIdx;

    @NotBlank(message = "配置名称不能为空")
    @Column(name = "name", nullable = false)
    private String name;

    @NotBlank(message = "配置内容不能为空")
    @Column(name = "content", length = -1)
    private String content;

    @NotNull(message = "当前状态不能为空")
    @Min(value = 0, message = "当前状态'值'错误")
    @Max(value = 1, message = "当前状态'值'错误")
    @Column(name = "status", nullable = false)
    private Integer status;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    @Column(name = "create_by", nullable = false, length = 64)
    private String createBy = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    @Column(name = "update_by", nullable = false, length = 64)
    private String updateBy = "";

    @Column(name = "remark", nullable = false)
    private String remark = "";

    public void edit(SysConfig sysConfig) {
        this.uniqIdx = sysConfig.uniqIdx;
        this.name = sysConfig.name;
        this.content = sysConfig.content;
        this.status = sysConfig.status;
        this.remark = sysConfig.remark;
    }
}
